package com.weihuachao.https;

import android.content.Context;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

/**
 * Created by Virgo on 2016/7/30.
 */
public class XUtils {
    public static final String U = "http://localhost:8080/college_online/";
    public static final String LOGIN = "login";
    public static final String REGISTER="register";
    public static final String UPDATE="update";
    public static final String GETWORK="getwork";
    public static final String ADDWORK="addwork";
    public static final String UPDATEWORK="updatework";
    private static BitmapUtils bitmapUtils;
    private static HttpUtils httpUtils;
    private static Context context;
    private static DbUtils dbUtils;
    private static HttpHandler handler;

    public static void init(Context mcontext) {
        if (bitmapUtils == null) {
            bitmapUtils = new BitmapUtils(mcontext);
            bitmapUtils.configDiskCacheEnabled(true);
        }
        if (httpUtils == null) {
            httpUtils = new HttpUtils();
        }
        if (dbUtils == null) {
            dbUtils = DbUtils.create(mcontext, "college.db");
        }
        context = mcontext;
    }

    public static <T> void send(String url, RequestParams params, RequestCallBack<T> callBack) {
        if (params == null) {
            handler = httpUtils.send(HttpRequest.HttpMethod.GET, U + url, callBack);
        } else {
            handler = httpUtils.send(HttpRequest.HttpMethod.POST, U + url, params, callBack);
        }
    }

    public static void cancel() {
        if (handler != null) {
            handler.cancel();
            handler = null;
        }
    }

    public static void display(ImageView imageView, String url) {
        bitmapUtils.display(imageView, url);
    }

    public static void show(String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }
}
